<?php
session_start();
require_once("registereduserobj.php");
(isset($_REQUEST["sub"])) ? $sub = $_REQUEST["sub"] : $sub = "NULL" ;
(isset($_REQUEST["code"])) ? $code = $_REQUEST["code"] : $code = "NULL" ;
if ($code !== "NULL") {
 $obj = new registereduser();
 $obj->applyfromuser();
 if (isset($_REQUEST["i8"])) {
  $obj->applyfromrequest($code);
  if ($obj->valid) {
   confirmreset();
  } else {
   showform($obj);
  }
 } else {
  showform($obj);
 }
}
 
function confirmreset() {
 echo "Changes made as requested. Click <a href=menu.php>here</a>.";
}

function showform($obj) {

 $i = $obj->header;
 
 echo "<h1>Reset Password</h1>";
 echo "<h2>$i[1]</h2>";
 echo "<form name='form' method=post>";
 echo "<table>";
 
 echo "<tr>";
 echo "<td>New Password</td>";
 echo "<td>";
 echo "<input type=password name=f5>";
 echo "</td>";
 echo "</tr>";

 echo "<tr>";
 echo "<td>New Password (again)</td>";
 echo "<td>";
 echo "<input type=password name=i5>";
 echo "</td>";
 echo "<td>";
 echo $obj->valids[5];
 echo "</td>";
 echo "</tr>";

 // Write out a captcha
 $im = imagecreate(82, 20) or die("Cannot Initialize new GD image stream");
 $background_color = imagecolorallocate($im, 223,234,248);
 $text_color = imagecolorallocate($im, 76,147,233);
 $p = generatepassword(); 
 $_SESSION["captcha"] = md5($p);
 imagestring($im, 5,4, 0,  $p, $text_color); 
 $im = imagerotate($im,mt_rand(0,89)-44,$background_color);
 $fn = "captcha\\".md5($p).".png";
 imagepng($im,$fn);
 imagedestroy($im);

 echo "<tr>";
 echo "<td align=center>Word verification.<br> Type the characters you see below.<br>";
 echo "<img src='$fn'>";
 echo "</td>";
 echo "<td>";
 echo "<input name=i8>";
 echo "</td>";
 echo "<td>";
 echo $obj->valids[8];
 echo "</td>";
 echo "</tr>";

 echo "</table>";
 echo "<input type=submit name=sub value=Save>";
 echo "</form>"; 
 }
?>